import SDK from "./NIM_Web_SDK_weixin_v6.2.0";
import {
    showToast
}
from '../utils/util.js';

let app = getApp()
let store = app.store

let orderCounter = 1
export default class ChatroomController {
    constructor(headers) {
        console.log(headers);
        app.globalData.chatroom = SDK.Chatroom.getInstance({
            debug: true,
            appKey: app.globalData.appkey,
            token: app.globalData.wytoken,
            account: app.globalData.account,
            chatroomAddresses: headers.data.address,
            chatroomId: headers.data.chatroomId,
            onconnect: this.onChatroomConnect,
            onerror: this.onChatroomError,
            onwillreconnect: this.onChatroomWillReconnect,
            ondisconnect: this.onChatroomDisconnect,
            // 消息
            onmsgs: this.onChatroomMsgs

        });
    }
    /** 1
     * 连接成功
     */
    onChatroomConnect(obj) {
        console.log('进入聊天室', obj);
        // 连接成功后才可以发消息
        var msg = app.globalData.chatroom.sendText({
            text: 'hello',
            done: function sendChatroomMsgDone(msgObj) {}
        })
    };

    onChatroomError(error, obj) {
        console.log('发生错误', error, obj);
    };

    onChatroomWillReconnect(obj) {
        // 此时说明 `SDK` 已经断开连接, 请开发者在界面上提示用户连接已断开, 而且正在重新建立连接
        console.log('即将重连', obj);
    }

    onChatroomDisconnect(error) {
        // 此时说明 `SDK` 处于断开状态, 开发者此时应该根据错误码提示相应的错误信息, 并且跳转到登录页面
        console.log('连接断开', error);
        if (error) {
            switch (error.code) {
                // 账号或者密码错误, 请跳转到登录页面并提示错误
                case 302:
                    break;
                    // 被踢, 请提示错误后跳转到登录页面
                case 'kicked':
                    break;
                default:
                    break;
            }
        }
    }

    onChatroomMsgs(msgs) {
        console.log('收到聊天室消息', msgs);
    }

}